package zhengqc.set;

import zhengqc.common.Pair;
import zhengqc.hash_table.HashTable;
import zhengqc.map.HashMap;

import java.util.Arrays;

public class HashSet<E extends Comparable<E>> implements Set<E> {
    private HashTable<E, Object> hashTable;

    public HashSet() {
        this.hashTable = new HashTable<>();
    }

    @Override
    public void add(E element) {
        this.hashTable.add(element, null);
    }

    @Override
    public void remove(E element) {
        this.hashTable.remove(element);
    }

    @Override
    public boolean contains(E element) {
        return this.hashTable.contains(element);
    }

    @Override
    public int getSize() {
        return this.hashTable.getSize();
    }

    @Override
    public boolean isEmpty() {
        return this.hashTable.getSize() == 0;
    }

    @Override
    public String toString() {
        Pair<E, Object>[] items = this.hashTable.toItems();
        StringBuilder sb = new StringBuilder("[").append(items[0].first);
        for (int i = 1; i < items.length; ++i) {
            sb.append(", ").append(items[i].first);
        }
        return sb.append("]").toString();
    }
}
